package com.yx.utils;

import java.text.ParseException;
import java.text.ParsePosition;
import java.text.SimpleDateFormat;
import java.util.*;

/**
 * DATE处理工具类
 *
 * @author xiaoyu
 */
public class DateUtil {
    public static int daysBetween(Date firstDate, Date secondDate) {
        Calendar cal = Calendar.getInstance();
        cal.setTime(firstDate);
        long time1 = cal.getTimeInMillis();
        cal.setTime(secondDate);
        long time2 = cal.getTimeInMillis();
        long between_days = (time2 - time1) / (1000 * 3600 * 24);

        return Integer.parseInt(String.valueOf(between_days));
    }

    public static String DateToMySQLDateTimeString(Date date) {
        final String[] MONTH = {
                "Jan", "Feb", "Mar", "Apr", "May", "Jun",
                "Jul", "Aug", "Sep", "Oct", "Nov", "Dec",
        };
        StringBuffer ret = new StringBuffer();
        String dateToString = date.toString();  //like "Sat Dec 17 15:55:16 CST 2005"
        ret.append(dateToString.substring(24, 24 + 4));//append yyyy
        String sMonth = dateToString.substring(4, 4 + 3);
        for (int i = 0; i < 12; i++) {      //append mm
            if (sMonth.equalsIgnoreCase(MONTH[i])) {
                if ((i + 1) < 10) {
					ret.append("-0");
				} else {
					ret.append("-");
				}
                ret.append((i + 1));
                break;
            }
        }
        ret.append("-");
        ret.append(dateToString.substring(8, 8 + 2));
        ret.append(" ");
        ret.append(dateToString.substring(11, 11 + 8));

        return ret.toString();
    }

    public static Date getNowDate() {
        Date currentTime = new Date();
        SimpleDateFormat formatter = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
        String dateString = formatter.format(currentTime);
        ParsePosition pos = new ParsePosition(8);
        Date currentTime_2 = formatter.parse(dateString, pos);
        return currentTime_2;
    }


    public static Date DateTime(Date time) {
        Calendar c = Calendar.getInstance();
        c.setTime(time);
        c.add(Calendar.HOUR_OF_DAY, 24);
        String x = c.getTime().toString();
        SimpleDateFormat sdf1 = new SimpleDateFormat("EEE MMM dd HH:mm:ss Z yyyy", Locale.UK);
        try {
            Date date = sdf1.parse(x);
            return date;
        } catch (ParseException e) {
            e.printStackTrace();
            return null;
        }
    }

    public static Date DateTime1(Date time) {
        Calendar c = Calendar.getInstance();
        c.setTime(time);
        c.add(Calendar.HOUR_OF_DAY, 1);
        String x = c.getTime().toString();
        SimpleDateFormat sdf1 = new SimpleDateFormat("EEE MMM dd HH:mm:ss Z yyyy", Locale.UK);
        try {
            Date date = sdf1.parse(x);
            return date;
        } catch (ParseException e) {
            e.printStackTrace();
            return null;
        }
    }

    //获取年
    public static Integer getYear(Date time){
        Calendar c = Calendar.getInstance();
        c.setTime(time);
        Integer year = c.get(Calendar.YEAR);
        return  year;
    }

    //获取月
    public static Integer getMonth(Date time){
        Calendar c = Calendar.getInstance();
        c.setTime(time);
        Integer month = c.get(Calendar.MONTH)+1;
        return month;
    }

    //获取当月当前周
    public static Integer getWeek(Date date){
        Calendar c = Calendar.getInstance();
        c.setTime(date);
        Integer week = c.get(Calendar.WEEK_OF_MONTH);
        return week;
    }

    //获取当前天
    public static Integer getDay(Date time){
        Calendar c = Calendar.getInstance();
        c.setTime(time);
        Integer day = c.get(Calendar.DAY_OF_MONTH);
        return day;
    }

    //判断月份的近五月
    public static Map<String,Object> month(int month, int year){
        Map<String,Object> map = new HashMap<>();
        if (month==0){
            map.put("month",12);
            map.put("year",year-1);
            return map;
        }
        if (month==-1){
            map.put("month",11);
            map.put("year",year-1);
            return map;
        }
        if (month==-2){
            map.put("month",10);
            map.put("year",year-1);
            return map;
        }
        if (month==-3){
            map.put("month",9);
            map.put("year",year-1);
            return map;
        }
        map.put("month",month);
        map.put("year",year);
        return map;
    }

   public static void main(String[] args){
        Date date = new Date();
        Calendar calendar = Calendar.getInstance();
        calendar.setTime(date);
        int month = calendar.get(Calendar.MONTH)+1;
        int year = calendar.get(Calendar.YEAR);
        int day = calendar.get(Calendar.DAY_OF_MONTH);
        int week = calendar.get(Calendar.WEEK_OF_MONTH);
        System.out.println(day);
        System.out.println(week);
        System.out.println(month);
        System.out.println(year);
    }
}
